package com.baizhi.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.entity.Guru;
import com.baizhi.service.GuruService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by 野马 on 2019/6/17.
 */
@Controller
@RequestMapping("/guru")
public class GuruController {

    @Autowired
    private GuruService guruService;


    //查询全部上师
    @RequestMapping("/selectGuru")
    @ResponseBody
    public Map selectGuru(String sguruName,int page,int rows){
        System.out.println(page+rows+"分页");
        System.out.println(sguruName+"123456789");
        return guruService.selectGuru(sguruName, page, rows);
    }

    //添加上师
    @RequestMapping("/insertGuru")
    @ResponseBody
    public Map insertGuru(String guruName, String guruNickName, MultipartFile touxiang, HttpServletRequest request) throws Exception {


        //获取文件名字
        String filename = touxiang.getOriginalFilename();

        String str = UUID.randomUUID().toString();

        String newName = str + filename.substring(filename.lastIndexOf("."));
        Guru guru = new Guru();
        guru.setGuruName(guruName);
        guru.setGuruNickName(guruNickName);
        guru.setGuruImage(newName);

        Map map = new HashMap();
        try {
            //把接收的文件写入到服务器磁盘
            String realPath = request.getRealPath("img");
            File file = new File(realPath+"/"+newName);
            touxiang.transferTo(file);
            guruService.insertGuru(guru);
            map.put("guruinsert",true);
        } catch (Exception e) {
            map.put("guruinsert",false);
            e.printStackTrace();
        }


        return map;

    }
    //导入excel表格
    @RequestMapping("/daoru")
    @ResponseBody
    public Map daoru(@RequestParam("excel")MultipartFile excel) throws Exception {
        System.out.println("111111"+excel.getOriginalFilename()+"excel地址11111");
        String excell = "E://"+excel.getOriginalFilename();
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);

        FileInputStream fileInputStream = new FileInputStream(new File(excell));
        List<Guru> guruList = ExcelImportUtil.importExcel(fileInputStream, Guru.class, params);

        Map map = new HashMap();
        try {
            for (Guru guru : guruList) {
                guruService.insertGuru(guru);
            }
            map.put("daoru",true);
        } catch (Exception e) {
            map.put("daoru",false);
            e.printStackTrace();
        }
        return map;
    }


    //导出excel表格
    @RequestMapping("/daochu")
    public void daochu() throws IOException {
        List<Guru> guruList = guruService.selectList();

        Workbook workbook = ExcelExportUtil.exportBigExcel(new ExportParams("上师表的所有数据","gurus"),Guru.class,guruList);

        workbook.write(new FileOutputStream(new File("E://easyguru.xls")));


    }


}
